package logic

import (
	"context"
	"errors"
	"novel-world/model/mysql"

	"novel-world/novelrpc/internal/svc"
	"novel-world/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type NovelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNovelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NovelLogic {
	return &NovelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *NovelLogic) Novel(in *novelrpc.NovelRequest) (*novelrpc.NovelResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.Novel
	if err := user.NovelFind(mysql.DB, in.Name); err == nil {
		return nil, errors.New("小说已存在")
	}
	data := &mysql.Novel{
		Name:    in.Name,
		Type:    in.Type,
		Label:   in.Label,
		Content: in.Content,
		UserId:  int(in.UserId),
	}
	if err := data.NovelAdd(mysql.DB); err != nil {
		return nil, err
	}
	return &novelrpc.NovelResponse{
		NovelId: int64(data.ID),
	}, nil
}
